package com.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.generator.entity.Route;
import com.generator.mapper.PermissionMapper;
import com.generator.mapper.RouteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Semaphore;

/**
 * @author htj
 * @since 2021-10-01
 * 配置接口模块
 */
@CrossOrigin
@RestController
@RequestMapping("/config")
@ResponseBody
public class ConfigController {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RouteMapper routeMapper;

    //引用全局唯一的存储并发数的map
    @Autowired
    private HashMap<Integer, Semaphore> apiConcurrentMap;

    //引用全局唯一的存储超时时间的map
    @Autowired
    private HashMap<Integer, Integer> apiOvertimeMap;

    //引用全局唯一的存储开关的map
    @Autowired
    private HashMap<Integer, Integer> apiSwitchMap;

    //查询所有接口
    @GetMapping("/getAPI")
    public List<Route> getAPI() {
        List<Route> routeList = routeMapper.selectList(null);
        return routeList;
    }

    //添加接口
    @PostMapping("/insertAPI/{api_name}/{concurrent}/{overtime}/{availble}")
    public String insertAPI(@PathVariable("api_name") String api_name,
                            @PathVariable("concurrent") Integer concurrent,
                            @PathVariable("overtime") Integer overtime,
                            @PathVariable("availble") Integer availble) {
        //添加接口
        Route route = new Route();
        route.setApiName(api_name);
        route.setConcurrent(concurrent);
        route.setOvertime(overtime);
        route.setAvailable(availble);
        routeMapper.insert(route);

        int newID = route.getId();//返显，获取新的自增id

        //在map中添加该接口的最大并发数
        Semaphore semaphore = new Semaphore(concurrent);
        apiConcurrentMap.put(newID, semaphore);

        //在map中添加该接口的超时时间
        apiOvertimeMap.put(newID, overtime);

        //在map中添加该接口的开关
        apiSwitchMap.put(newID, availble);

        return "成功添加接口！";
    }

    //修改接口
    @PutMapping("/updateAPI/{api_id}/{concurrent}/{overtime}/{availble}")
    public String updateNote(@PathVariable("api_id") Integer api_id,
                             @PathVariable("concurrent") Integer concurrent,
                             @PathVariable("overtime") Integer overtime,
                             @PathVariable("availble") Integer availble) {
        Route route = routeMapper.selectById(api_id);
        route.setConcurrent(concurrent);
        route.setOvertime(overtime);
        route.setAvailable(availble);
        routeMapper.updateById(route);
        //在这里修改并发数，超时时间，开关的hashmap不是很方便么？也是1秒钟的事，也能做到配置实时更新。感觉没必要定时任务
        return "成功修改接口！";
    }


    //删除接口
    @DeleteMapping("/deleteAPI/{api_id}")
    public String deleteAPI(@PathVariable("api_id") Integer api_id) {
        //删除该接口的Semaphore
        apiConcurrentMap.remove(api_id);

        //删除该接口的超时时间
        apiOvertimeMap.remove(api_id);

        //在map中删除该接口的开关
        apiSwitchMap.remove(api_id);

        //先删除依赖表
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("api_id", api_id);
        permissionMapper.delete(queryWrapper);

        //最后删除接口
        routeMapper.deleteById(api_id);

        return "成功删除接口！";
    }

}

